<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class UserCenterController extends Controller
{

    /**
     * 用户中心
     * 
     * [UserCenter description]
     */
    public function getCenter(Request $request)
    {
        $id = session('id');
        $username = session('username');
        
        if(empty($id && $username)){
            return redirect('/');
        }
        //查询个人信息
        $users = DB::table('users')
                    ->join('accounts','users.id','=','accounts.uid')
                    ->select('users.*','accounts.money')
                    ->where('users.id','=',$id)
                    ->get();

        //查询订单是否存在(已经支付的订单)
        $datetime = time();
        $shijian = '2592000';
        $a = $datetime - $shijian;

        $res = DB::table('orders')
                ->where('uid',$id)
                ->where('orderstatus','=','1')
                ->where('orders.createtime','>',$a)
                ->orderBy('id','desc')
                ->select()
                ->get();
        // $res = DB::select("select * from orders where uid='$id' and status=1");
        // dd($res);
        //定义空数组 用来存值
        $orderses = [];
        if(!empty($res)){
            foreach($res as $k){
                foreach($k as $key){
                    $ress = DB::table('orderdetails')
                        ->join('orders','orderdetails.oid','=','orders.id')
                        ->join('goods','orderdetails.gid','=','goods.id')
                        ->join('shops','orderdetails.sid','=','shops.id')
                        ->where('orderdetails.oid','=',$k->id)
                        // ->select('goods.*')
                        ->select('orders.money','orders.id as oid','orders.createtime','orderdetails.count as ocount','shops.shopname','goods.goodsname','shops.logo')
                        ->first();
                    // dd($ress);
                }
                $orderses[$k->id]=$ress; 
            }
        }

         return view('home.UserCenter.center',['userss'=>$users,'orderses'=>$orderses]);
    }

    /**
     * 近三个月订单页面
     * 
     * [getOrder description]
     * @return [type] [description]
     */

    public function getOrder(Request $request)
    {
        $id = Session('id');
        $datetime = time();
        $shijian = '2592000';
        $a = $datetime - $shijian;

        $res = DB::table('orders')
                ->where('uid',$id)
                ->where('orders.createtime','>',$a)
                ->orderBy('id','desc')
                ->select()
                ->get();
        // dd(strtotime('2010-10-10')); 
        //定义空数组 用来存值
        $orders = [];
        if(!empty($res)){
            foreach($res as $k){
              
                  foreach($k as $key){
                    
                    $ress = DB::table('orderdetails')
                        ->join('orders','orderdetails.oid','=','orders.id')
                        ->join('goods','orderdetails.gid','=','goods.id')
                        ->join('shops','orderdetails.sid','=','shops.id')
                        ->where('orderdetails.oid','=',$k->id)
                        ->select('orders.money','orders.id as oid','orders.ordernum','orders.createtime','orders.orderstatus','orderdetails.count as ocount','shops.shopname','goods.goodsname','shops.logo')
                        ->get();
                        if(empty($ress)){
                            continue;
                        }
                    }
                    $orders[$k->id]=$ress; 
            }
        }
        
        return view('/home/userCenter/order',['orders'=>$orders]);
    }

    /**
     *待评价订单页面
     * 
     * [getUnassess description]
     * @return [type] [description]
     */
    public function getUnassess()
    {
        return view('/home/UserCenter/unassess');
    }

    /**
     * 退单记录页面
     * 
     * [getBackorder description]
     * @return [type] [description]
     */
    public function getBackorder()
    {
        return view('/home/UserCenter/backorder');
    }

    /**
     *账户余额页面
     * 
     * [getAccount description]
     * @return [type] [description]
     */
    public function getAccount()
    {
        return view('/home/UserCenter/account');
    }

    public function getScore()
    {
        return view('/home/UserCenter/score');
    }

    /**
     *个人资料页面
     * 
     * [getInfo description]
     * @return [type] [description]
     */
    public function getInfo()
    {
        $id = session('id');
        $users = DB::select("select * from users where id='$id'");
        

        return view('home/UserCenter/info',['users'=>$users]);
    }

    /**
     *用户地址信息页面
     * 
     * [getAddress description]
     * @return [type] [description]
     */
    public function getAddress()
    {

        $id = session('id');
        $address = DB::select("select * from addresses where uid='$id'");
        return view('home/UserCenter/address',['address'=>$address]);
    }

    /**
     * 用户安全页面
     * 
     * [getSecurity description]
     * @return [type] [description]
     */
    public function getSecurity()
    {
        $info = DB::table('users')->where('id','=',session('id'))->select()->first();
        $phone = substr_replace($info->phone, '****', 3, 4);
        // dd($phone);
        $str = substr($info->email,'0',3);
       
        $email = preg_replace('/[A-Z0-9._%+-]+@/',$str.'****@',$info->email);
         
        // $email = substr_replace($info->email,'****',)
        return view('home/UserCenter/security',['phone'=>$phone,'email'=>$email]);
    }

    /**
     * 修改密码页面
     * 
     * [getPassword description]
     * @return [type] [description]
     */
    public function getPassword()
    {
        return view('home/UserCenter/password');
    }

    /**
     *个人收藏页面
     * 
     * [getCollection description]
     * @return [type] [description]
     */
    public function getCollection()
    {

        // DB::select("select * from collect where id=session('id')");
        return view('home/UserCenter/collection');
    }

}
